//
//  ZDrawboadView.m
//  ZZBlue
//
//  Created by stonesMonkey on 2017/9/19.
//  Copyright © 2017年 stonesMonkey. All rights reserved.
//

#import "ZDrawboadView.h"
#import "UIView+ZUtilities.h"
#import "UIView+ColorOfPoint.h"
#import "ZZCollectColorTool.h"
#import "AppModel.h"

@interface  ZDrawboadView()

@property (nonatomic, strong)UIButton *viewA;

@property (nonatomic, strong)UIButton *viewB;

@property (nonatomic, assign)CGPoint panStartPointA;

@property (nonatomic, assign)CGPoint panStartPointB;

@property (weak, nonatomic) IBOutlet UIImageView *imgView;



@end

@implementation ZDrawboadView

- (void)awakeFromNib {
    [super awakeFromNib];
    
    [self setupSubView];
}





#pragma mark - action
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
    
}

- (void)btn1Move:(UIPanGestureRecognizer *)pan {

    self.isB = NO;
    CGPoint  point = [pan translationInView:self];
    CGFloat width = self.width - self.viewB.width / 2;
    CGFloat height = self.height - self.viewB.height / 2;
    CGRect originalFrame = self.viewA.frame;
    
    originalFrame.origin.x += point.x;
    originalFrame.origin.y += point.y;
    
    // 圆心
    CGPoint center = CGPointMake(CGRectGetMidX(originalFrame) - width / 2,CGRectGetMidY(originalFrame) - width /2);
    
    CGFloat offetX  = self.viewA.width / 2;
    CGFloat offerY = self.viewA.height / 2;
    if (center.x > 0) {
        
        offetX = offetX / 2;
    }
    if(center.y > 0) {
        
        offerY = offerY / 2;
    }
    
    CGFloat x2 = fabs(center.x - offetX ) * fabs(center.x - offetX);
    CGFloat y2 = fabs(center.y - offerY)  * fabs(center.y  - offerY) ;
    CGFloat length = sqrt(x2 + y2);
    if (length >= width / 2) {
        
        originalFrame = self.viewA.frame;
    }
    
    self.viewA.frame = originalFrame;
    kAppModel.homeAReact = originalFrame;
    [kAppModel save];
    [pan setTranslation:CGPointZero inView:self];
    
    switch (pan.state) {

        case UIGestureRecognizerStateBegan:{

            self.viewA.size = CGSizeMake(40, 40);
            self.viewA.layer.cornerRadius = 20;
            self.viewB.size = CGSizeMake(28, 28);
            self.viewB.layer.cornerRadius = 14;
            kAppModel.homeBReact = self.viewB.frame;
            kAppModel.homeAReact = self.viewA.frame;
            [kAppModel save];
        }
            break;

        case UIGestureRecognizerStateEnded:
        {
            UIColor*color = [self colorOfPoint:self.viewA.center];
            [[[ZZCollectColorTool new] group].colors addObject:color];


            [[[ZZCollectColorTool new] group].reacts addObject:NSStringFromCGRect(originalFrame)];
            if ([[ZZCollectColorTool new] group].colors.count > 100) {

                [[[ZZCollectColorTool new] group].colors removeObjectAtIndex:0];
                [[[ZZCollectColorTool new] group].reacts removeObjectAtIndex:0];
            }
            [[ZZCollectColorTool new] save];
            
#pragma 音乐模式这里输出颜色
            // 输出当前ImageView上的颜色值
            
            
            
            if (self.viewDidClick) {
                
                self.viewDidClick(color,A);
            }
            
        }
            break;
       case UIGestureRecognizerStateChanged:
        case UIGestureRecognizerStateFailed:
        case UIGestureRecognizerStateCancelled:
        {
            // 输出当前ImageView上的颜色值
            UIColor*color = [self colorOfPoint:self.viewA.center];
            
            if (self.viewDidClick && self.isOutPut == NO) {
                
                self.viewDidClick(color,A);
            }
            
        }
            break;
            
        default:
            break;
    }
}

- (void)btn2Move:(UIPanGestureRecognizer *)pan {

    self.isB = YES;
    CGPoint  point = [pan translationInView:self];
    CGFloat width = self.width - self.viewB.width / 2;
    
    CGRect originalFrame = self.viewB.frame;
    
    originalFrame.origin.x += point.x;
    originalFrame.origin.y += point.y;
    
    // 圆心
    CGPoint center = CGPointMake(CGRectGetMidX(originalFrame) - width / 2,CGRectGetMidY(originalFrame) - width /2);
    
    CGFloat offetX  = self.viewB.width / 2;
    CGFloat offerY = self.viewB.height / 2;
    if (center.x > 0) {
        
        offetX = offetX / 2;
    }
    if(center.y > 0) {
        
        offerY = offerY / 2;
    }
    
    CGFloat x2 = fabs(center.x - offetX ) * fabs(center.x - offetX);
    CGFloat y2 = fabs(center.y - offerY)  * fabs(center.y  - offerY) ;
    CGFloat length = sqrt(x2 + y2);
    if (length >= width / 2) {
        
        originalFrame = self.viewB.frame;
    }
    
    self.viewB.frame = originalFrame;
    kAppModel.homeBReact = originalFrame;
    [kAppModel save];
    [pan setTranslation:CGPointZero inView:self];
    
    switch (pan.state) {
        case UIGestureRecognizerStateBegan:{

            self.viewB.size = CGSizeMake(40, 40);
            self.viewB.layer.cornerRadius = 20;
            self.viewA.size = CGSizeMake(28, 28);
            self.viewA.layer.cornerRadius = 14;

            kAppModel.homeBReact = self.viewB.frame;
            kAppModel.homeAReact = self.viewA.frame;
            [kAppModel save];
        }
            break;


        case UIGestureRecognizerStateEnded:
        {
            
            UIColor*color = [self colorOfPoint:self.viewB.center];
#pragma 音乐模式下输出B的颜色
            [[[ZZCollectColorTool new] group].colors addObject:color];

            NSLog(@"AAAAB = %@",NSStringFromCGRect(originalFrame));
            [[[ZZCollectColorTool new] group].reacts addObject:NSStringFromCGRect(originalFrame)];
            if ([[ZZCollectColorTool new] group].colors.count > 100) {

                [[[ZZCollectColorTool new] group].colors removeObjectAtIndex:0];
                [[[ZZCollectColorTool new] group].reacts removeObjectAtIndex:0];
            }
            [[ZZCollectColorTool new] save];
            if (self.viewDidClick) {
                
                self.viewDidClick(color,B);
            }
        }
            break;
            case UIGestureRecognizerStateChanged:
        case UIGestureRecognizerStateFailed:
        case UIGestureRecognizerStateCancelled:
        {
            // 输出当前ImageView上的颜色值
            UIColor*color = [self colorOfPoint:self.viewB.center];
            
            if (self.viewDidClick && self.isOutPut == NO) {
                
                self.viewDidClick(color,B);
            }
            NSLog(@"%@",color);
        }
            break;
            
        default:
            break;
    }
}

#pragma mark - setupSubView
- (void)setupSubView {
    
    self.viewA = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.viewA setTitle:@"A" forState:UIControlStateNormal];
    
    self.viewA.titleLabel.font = [UIFont systemFontOfSize:12];
    [self.viewA setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self addSubview:_viewA];
    _viewA.frame = CGRectMake(50, 50, 40,40);
    _viewA.layer.cornerRadius = 20;
    _viewA.layer.borderWidth = 2;
    _viewA.layer.borderColor = [UIColor whiteColor].CGColor;
    _viewA.layer.masksToBounds = YES;
    self.viewA.titleLabel.font = [UIFont systemFontOfSize:17];
    
    UIPanGestureRecognizer *pan1 = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(btn1Move:)];
    [_viewA addGestureRecognizer:pan1];
    
    self.viewB = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.viewB setTitle:@"B" forState:UIControlStateNormal];
    UIPanGestureRecognizer *pan2 = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(btn2Move:)];
    self.viewB.titleLabel.font = [UIFont systemFontOfSize:17];
    [_viewB addGestureRecognizer:pan2];
    
    self.viewB.titleLabel.font = [UIFont systemFontOfSize:12];
    [self.viewB setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self addSubview:_viewB];
    _viewB.frame = CGRectMake(150, 150, 28, 28);
    _viewB.layer.cornerRadius = 14;
    _viewB.layer.borderWidth = 2;
    _viewB.layer.borderColor = [UIColor whiteColor].CGColor;
    _viewB.layer.masksToBounds = YES;
}

- (CGPoint)pointForColor:(UIColor *)color colorChangeIsB:(BOOL)isB {
    
    NSArray *rgna = [ZDrawboadView colorForGGB:color];
    for (int i = 0; i < self.bounds.size.width; i =  i + 10) {
        
        for (int j = 0; j < self.bounds.size.height; j = j+10) {
            
            CGPoint point = CGPointMake(i, j);
            UIColor *changeColor = [self colorOfPoint:point];
            
            NSArray *array = [ZDrawboadView colorForGGB:changeColor];
            
            if (fabs([array[0] floatValue] - [rgna[0] floatValue]) < 10 && [array[1] floatValue] - [rgna[1] floatValue] && [array[2] floatValue] - [rgna[2] floatValue]) {
                
                self.viewA.center = point;
                return point;
            }
        }
    }
    
    return CGPointZero;
}


#pragma mark - Func
- (void)drawRect:(CGRect)rect {
    
    UIBezierPath *path1 = [[UIBezierPath alloc] init];
    
}

// 返回颜色RBG值
+ (NSArray *)colorForGGB:(UIColor *)color {
    
    const CGFloat *components = CGColorGetComponents(color.CGColor);
    return @[@(components[0] * 255.0),@(components[1] * 255.0),@(components[2] * 255.0)];
}

- (UIColor *)colorOfPoint:(CGPoint)point {
    unsigned char pixel[4] = {0};
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef context = CGBitmapContextCreate(pixel, 1, 1, 8, 4, colorSpace, (CGBitmapInfo)kCGImageAlphaPremultipliedLast);
    
    CGContextTranslateCTM(context, -point.x, -point.y);
    
    [self.imgView.layer renderInContext:context];
    
    CGContextRelease(context);
    CGColorSpaceRelease(colorSpace);
    
    UIColor *color = [UIColor colorWithRed:pixel[0]/255.0 green:pixel[1]/255.0 blue:pixel[2]/255.0 alpha:pixel[3]/255.0];
    
    return color;
}

- (void)selectColor:(UIColor *)color forReact:(CGRect)react {

    if (self.isB) {

        self.viewB.frame = react;
    } else {

        self.viewA.frame = react;
    }
    NSLog(@"%@",NSStringFromCGRect(react));

}

#pragma mark - set & get
- (void)setARect:(CGRect)aReact BReact:(CGRect)bReact {

    self.viewA.frame = aReact;
    if (aReact.size.width == 40) {

        self.viewA.layer.cornerRadius = 20;
         self.viewB.layer.cornerRadius = 14;
    } else {

        self.viewA.layer.cornerRadius = 14;
        self.viewB.layer.cornerRadius = 20;
    }
    self.viewB.frame = bReact;
}

@end

